<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Subject extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
    }

    public function index()
    {
        if (!$this->tank_auth->is_logged_in())
        {
            redirect('/auth/login/');
        }
        
        $data['user_id']	= $this->tank_auth->get_user_id();
        $data['user_login']	= $this->tank_auth->get_username();
        // Set the validation rules if this is a submit
        if ( $this->input->post('action') == 'submit' )
        {

            $this->form_validation->set_rules('subject_code', 'Subject Code', 'trim|required|min_length[4]|max_length[12]|xss_clean');
            $this->form_validation->set_rules('subject_name', 'Subject Name', 'trim|required');
            $this->form_validation->set_rules('subject_description', 'Subject Description', 'trim');
            $this->form_validation->set_rules('subject_unit', 'Subject Unit', 'trim|required');

            if ( ! $this->form_validation->run() )
            {
                $this->smarty->assign('error', 'check_form_errors');
            }
            else
            {
                if ($this->em->getRepository('models\SubjectInfo')->findOneBy(array('subject_info_code' => $_POST['subject_code'])))
                {
                    $this->smarty->assign('error', 'subject_code_exist');
                }
                else
                {
                    $subject_info = new \models\SubjectInfo;
                
                    $subject_info->setSubjectInfoCode($_POST['subject_code']);
                    $subject_info->setSubjectInfoName($_POST['subject_name']);
                    $subject_info->setSubjectInfoUnit($_POST['subject_unit']);
                
                    if($_POST['subject_description'])
                    {
                        $$subject_info->setSubjectInfoDescription($_POST['subject_description']);
                    }
                
                    $this->em->persist($subject_info);
                    $this->em->flush();
                    
                    $this->smarty->assign('subject_id', $_POST['subject_id']);
                    $this->smarty->assign('message', 'Thank you for posting!');
                }
            }
        }
        
        // These assignments are passed by the associative array
        $data['title'] = 'Welcome to the Smarty Website';
        $this->smarty->assign('add_subject_info', true);
        // Load the template from the views directory
        $this->smarty->view("subject.tpl", $data);
    }
    
    public function prerequisites()
    {
        if (!$this->tank_auth->is_logged_in())
        {
            redirect('/auth/login/');
        }
        
        $data['user_id']	= $this->tank_auth->get_user_id();
        $data['user_login']	= $this->tank_auth->get_username();
        
        if ( $this->input->post('action') == 'submit' )
        {
            $_subject_id  = $_POST['subject_id'];
            $_subject_obj = $this->em->find('models\Subject', $_subject_id);
            
            $subject_code_array = explode(", ", $_POST['subject_prerequisites']);
            
            $prerequisite_list = array();
            foreach($subject_code_array as $subject_code)
            {
                $subject_info_obj = $this->em->getRepository('models\SubjectInfo')->findOneBy(array('subject_info_code' => $subject_code));
                
                $subject_obj = $this->em->getRepository('models\Subject')->findOneBy(array('subject_info_id' => $subject_info_obj->getSubjectInfoId(), 'prospectus_id' => 1));
                
                $_subject_obj->addPrerequisite($subject_obj);
            }
            
            $this->em->flush();
        }
        
        $subject_objs = $this->em->getRepository( 'models\Subject' )->findBy( array( 'prospectus_id' => 1 ) );
        foreach ($subject_objs as $s)
        {
            $subject_array[$s->getSubjectId()] = $s->getSubjectInfo()->getSubjectInfoCode();
        }
        asort($subject_array);
        
        $this->smarty->assign('subjects', $subject_array);
        $this->smarty->assign('set_prerequisites', true);
        $this->smarty->view("subject.tpl");
    }
    
    public function show_prerequisite()
    {
        if (!$this->tank_auth->is_logged_in())
        {
            redirect('/auth/login/');
        }
        
        $data['user_id']	= $this->tank_auth->get_user_id();
        $data['user_login']	= $this->tank_auth->get_username();
        
        if ( $this->input->post('action') == 'submit' )
        {
            $_subject_id  = $_POST['subject_id'];
            $_subject_obj = $this->em->find('models\Subject', $_subject_id);
            
            $_prerequisite_subjects = $_subject_obj->getPrerequisiteSubjects();
            
            $this->smarty->assign('subject_id', $_POST['subject_id']);
            $this->smarty->assign('prerequisite_subjects', $_prerequisite_subjects);
        }
        $subject_objs = $this->em->getRepository( 'models\Subject' )->findBy( array( 'prospectus_id' => 1 ) );
        foreach ($subject_objs as $s)
        {
            $subject_array[$s->getSubjectId()] = $s->getSubjectInfo()->getSubjectInfoCode();
        }
        asort($subject_array);
        
        $this->smarty->assign('subjects', $subject_array);
        $this->smarty->assign('show_prerequisite', true);
        $this->smarty->view("subject.tpl");

    }

    public function checkSubjectCode(models\Subject $subject)
    {
        print ($subject->getSubjectCode());
    }
}
